<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            根节点到叶子节点的路径和==targetSum
            思路：找出所有路径，判断和是否等于targetSum
            */
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                        left: {
                            val: 8,
                        },
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    left: {
                        val: 6,
                    },
                    right: {
                        val: 7,
                        left: {
                            val: 9,
                        },
                    },
                },
            }
            var hasPathSum = function (root, targetSum) {
                let result = []
                if (!root) return []
                function preOrder(cur, path) {
                    if (!cur.left && !cur.right) {
                        return result.push([...path])
                    }
                    if (cur.left) {
                        path.push(cur.left.val)
                        preOrder(cur.left, path)
                        path.pop()
                    }
                    if (cur.right) {
                        path.push(cur.right.val)
                        preOrder(cur.right, path)
                        path.pop()
                    }
                }
                preOrder(root, [root.val])
                console.log(result)
            }
            hasPathSum(tree)
        </script>
    </body>
</html>
